﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Filas
{
    public class Animations : CommomFunctions.Animations
    {
        /// <summary>
        /// tempRectangle -> an auxiliary rectangle that will have the properties of the rectangle that we want
        /// to be animated. It is the rectangle that will be in fact transformed during animation.
        /// r1 -> contains all the rectangles that will be animated
        /// </summary>
        static Rectangle tempRectangle = new Rectangle();
        static ArrayList r1 = new ArrayList();
        
        /// <summary>
        /// Animates the removal of a rectangle.
        /// "Simulates" the dislocation of all the remaning elements to the left.
        /// </summary>
        /// <param name="contMoves">
        /// The index of the rectangle which deslocation will be "simulated"
        /// </param>
        public static void animaRemove(int contMoves)
        {
            tempRectangle = new Rectangle();

            r1.Add(tempRectangle);
            Gridi.Children.Add((Rectangle)r1[r1.Count - 1]);
            Grid.SetRow(((Rectangle)r1[r1.Count - 1]), 2);
            Grid.SetRowSpan(((Rectangle)r1[r1.Count - 1]), 2);
            Grid.SetColumnSpan(((Rectangle)r1[r1.Count - 1]), 6);
            ((Rectangle)r1[r1.Count - 1]).Visibility = Visibility.Visible;
            ((Rectangle)r1[r1.Count - 1]).Fill = (ControlVariables.getRectangle(contMoves)).Fill;
            ((Rectangle)r1[r1.Count - 1]).Margin = new Thickness(ControlVariables.getRectangle(contMoves).Margin.Left, ControlVariables.getRectangle(contMoves).Margin.Top, ControlVariables.getRectangle(contMoves).Margin.Right, ControlVariables.getRectangle(contMoves).Margin.Bottom);
            ((Rectangle)r1[r1.Count - 1]).Height = 634 / 6.0;

            ThicknessAnimationUsingKeyFrames myThicknessAnimation = new ThicknessAnimationUsingKeyFrames();

            LinearThicknessKeyFrame A_C = new LinearThicknessKeyFrame(ControlVariables.getRectangle(contMoves - 1).Margin, KeyTime.Uniform);
            myThicknessAnimation.KeyFrames.Add(A_C);

            NameScope.SetNameScope(tempRectangle, new NameScope());
            tempRectangle.RegisterName("myAnimatedObject", ((Rectangle)r1[r1.Count - 1]));
            
            createStoryboard(myThicknessAnimation, Rectangle.MarginProperty, 2.0, myThicknessAnimation_Completed, tempRectangle);  
        }

        /// <summary>
        /// Executes after the animation is completed. 
        /// Removes all the temporary rectangles that were added to the grid and updates the
        /// properties of the elements, images and buttons.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void myThicknessAnimation_Completed(object sender, EventArgs e)
        {
            foreach (Rectangle r in r1)
            {
                ((Rectangle)r).Visibility = Visibility.Hidden;
                Gridi.Children.Remove(((Rectangle)r));
            }

            ControlVariables.endAnimaRemove();           
        }        
    }
}